package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.ChoiceActivity;
import flc.ast.adapter.HomeAdapter;
import flc.ast.databinding.FragmentHomeBinding;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.StatusBarUtils;
import stark.common.bean.StkResBean;
import stark.common.bean.StkTagResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private List<StkTagResBean> listType = new ArrayList<>();
    private HomeAdapter homeAdapter;
    private int page = 1;
    private int refreshTime = 200;
    private int type = 0;

    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.rvHomeList.setLayoutManager(new GridLayoutManager(mContext, 2));
        homeAdapter = new HomeAdapter();
        mDataBinding.rvHomeList.setAdapter(homeAdapter);
        homeAdapter.setOnItemClickListener(this);

        mDataBinding.refreshTabItem.setRefreshHeader(new ClassicsHeader(mContext));
        mDataBinding.refreshTabItem.setRefreshFooter(new ClassicsFooter(mContext));
        mDataBinding.refreshTabItem.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                if (type == 0) {
                    getData(listType.get(0).getHashid());
                } else if (type == 1) {
                    getData(listType.get(1).getHashid());
                } else if (type == 2) {
                    getData(listType.get(2).getHashid());
                }
                mDataBinding.refreshTabItem.finishLoadMore(refreshTime);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                if (type == 0) {
                    getData(listType.get(0).getHashid());
                } else if (type == 1) {
                    getData(listType.get(1).getHashid());
                } else if (type == 2) {
                    getData(listType.get(2).getHashid());
                }
                mDataBinding.refreshTabItem.finishRefresh(refreshTime);
            }
        });
        mDataBinding.ivChoice.setOnClickListener(this);
        mDataBinding.tvHot.setOnClickListener(this);
        mDataBinding.tvLatest.setOnClickListener(this);
        mDataBinding.tvLike.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivChoice://每周精选
                startActivity(ChoiceActivity.class);
                break;
            case R.id.tvHot://热门推荐
                clearSelection();
                type = 0;
                page = 1;
                getData(listType.get(0).getHashid());
                mDataBinding.tvHot.setBackgroundResource(R.drawable.bt_kp);
                break;
            case R.id.tvLatest://最新番剧
                clearSelection();
                type = 1;
                page = 1;
                getData(listType.get(1).getHashid());
                mDataBinding.tvLatest.setBackgroundResource(R.drawable.bt_kp);
                break;
            case R.id.tvLike://猜你喜欢
                clearSelection();
                type = 2;
                page = 1;
                getData(listType.get(2).getHashid());
                mDataBinding.tvLike.setBackgroundResource(R.drawable.bt_kp);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        BaseWebviewActivity.open(mContext, bean.getUrl(), bean.getName());
    }

    @Override
    protected void initData() {
        getTypeData();
    }

    private void getTypeData() {
        StkResApi.getChildTagList(null, AppConfig.HOME_DATA, StkResApi.createParamMap(0, 10), false, new IReqRetCallback<List<StkTagResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkTagResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        listType.addAll(result);
                        getData(listType.get(0).getHashid());
                    }
                }
            }
        });
    }

    private void getData(String hashId) {
        StkResApi.getTagResourceList(null, AppConfig.URL_PREFIX + hashId, StkResApi.createParamMap(0, 10), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        if (page == 1) {
                            homeAdapter.setList(result);
                        } else {
                            homeAdapter.addData(result);
                        }
                    }
                }
            }
        });
    }

    private void clearSelection() {
        mDataBinding.tvHot.setBackgroundResource(R.drawable.bt_kp2);
        mDataBinding.tvLatest.setBackgroundResource(R.drawable.bt_kp2);
        mDataBinding.tvLike.setBackgroundResource(R.drawable.bt_kp2);
    }

}